import Vue from "vue";
import store from "@/store/index";
import VueRouter from "vue-router"; // 引入路由
import LoginView from "@/views/LoginView.vue";
import HomeView from "@/views/HomeView.vue";
// import beautification from "@/views/beautification/beautification.vue";
import ProductionView from "@/views/Home/ProductionView.vue";
import { checkx, createx, finds } from "@/utils/index.js";

Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/login",
      component: LoginView,
    },
    {
      path: "/home",
      component: HomeView,
      name: "首页容器",
      children: [
        {
          name: "首页",
          path: "/home/index",
          component: () => import("@/views/Home/index.vue"),
        },
        // 用户
        {
          name:'用户',
          path: "/home/userlist",
          component: () => import("@/views/Home/User/UserList.vue"),
        },
        {
          //商品
          path: "/home/ProductionView",
          component: ProductionView,
          children: [
            {
              name: '商品列表',
              path: "/Home/Production/ProductionList",
              component: () =>
                import("@/views/Home/Production/ProductionList.vue"),
            },
            {
              name: '添加商品',
              path: "/Home/Production/AddProduction",
              component: () =>
                import("@/views/Home/Production/AddProduction.vue"),
            },
            {
              name: '查找商品',
              path: "/Home/Production/ProductionClassification",
              component: () =>
                import("@/views/Home/Production/ProductionClassification.vue"),
            },
          ],
        },
        {
          name: "美化",
          path: "/home/beautification",
          component: () => import("@/views/beautification/beautification.vue"),
        },
        {
          name:"订单",
          path: "/home/orders",
          component: () =>import("@/views/Home/orders/ProductionList.vue"),
        },
        {
          //权限
          path: "/home/admin",
          component: () => import("@/views/Home/AdminVue.vue"),
          children: [
            {
              // 权限列表
              name: "权限",
              path: "/home/admin/adminlist",
              component: () => import("@/views/Home/Adminer/AdminList.vue"),
            },
            {
              // 角色列表
              name: "角色",
              path: "/home/admin/powerlist",
              component: () => import("@/views/Home/Adminer/PowerList.vue"),
            },
          ],
        },
      ],
    },
    {
      // 如果是单斜杠 重定向到 登录
      path: "/",
      redirect: "/login",
    },
  ],
  mode: "history",
});
const VueRouterPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

router.beforeEach(function (to, from, next) {
  if (to.name) {
    if (checkx(store.state.abcList, to.fullPath) === -1) {
      store.commit("setAbcList", createx(to.fullPath, to.name));
    }
  }


  let qqq = store.state.abcList
  let num = finds(qqq, to.fullPath)
  store.commit("setAbcnum", num);
  // console.log(store.state.abcnum)
  next();
});
export default router;
